/********************************************************************
 * PROJECT: CSAIR ROST PROJECT.
 * SUPPLIER: FOREVER OPEN SOURCE SOFTWARE INC.
 ********************************************************************
 * FILE: GanttGridItem.java
 * (C) Copyright Foliage Team 2013, All Rights Reserved.
 ********************************************************************/
package org.foliage.rcp.ui.custom;

import org.eclipse.swt.widgets.Item;
import org.foliage.rcp.ui.widgets.GanttItem;
import org.foliage.rcp.ui.widgets.GanttTask;
import org.foliage.rcp.ui.widgets.GridItem;

/**
 * 带有报表功能的甘特图单元控件。
 * <ul>
 * <li>双向桥接甘特图和报表单元控件；</li>
 * <li>解耦甘特图和报表单元控件。</li>
 * </ul>
 * 
 * @author deathknight0718@gmail.com
 * @version 1.0.0
 */
public class GanttGridItem extends Item {

	// ----------------------------------------------- 

	public static final String DATA_KEY = GanttGridItem.class.getName();

	// ----------------------------------------------- 

	private final GanttItem ganttItem;

	private final GridItem gridItem;

	// ----------------------------------------------- 

	public GanttGridItem(GanttGrid parent, int style) {
		this(parent, style, -1);
	}

	public GanttGridItem(GanttGrid parent, int style, int index) {
		super(parent, style, index);

		this.ganttItem = new GanttItem(parent.getGantt(), style, index);
		this.ganttItem.setData(DATA_KEY, this);

		this.gridItem = new GridItem(parent.getGrid(), style, index);
		this.gridItem.setData(DATA_KEY, this);
	}

	public GanttGridItem(GanttGrid parent, int style, int index, GanttGridItemData data) {
		super(parent, style, index);

		this.ganttItem = new GanttItem(parent.getGantt(), style, index);
		this.ganttItem.setData(DATA_KEY, this);
		this.ganttItem.setText(data.getGanttText());

		this.gridItem = new GridItem(parent.getGrid(), style, index);
		this.gridItem.setData(DATA_KEY, this);

		this.setVisible(data.isVisible());

		String[] gridTexts = data.getGridTexts();
		for (int i = 0; i < gridTexts.length; i++) {
			this.gridItem.setText(i, gridTexts[i]);
		}

		GanttGridTaskData[] taskDatas = data.getTaskDatas();
		if (taskDatas != null) {
			for (int i = 0; i < taskDatas.length; i++) {
				new GanttGridTask(this, taskDatas[i]);
			}
		}
	}

	// ----------------------------------------------- 

	@Override
	public void dispose() {
		ganttItem.dispose();
		gridItem.dispose();
		super.dispose();
	}

	// ----------------------------------------------- 

	public String getGanttText() {
		return ganttItem.getText();
	}

	public void setGanttText(String text) {
		ganttItem.setText(text);
	}

	public String getGridText() {
		return gridItem.getText();
	}

	public void setGridText(String text) {
		gridItem.setText(text);
	}

	public String getGridText(int index) {
		return gridItem.getText(index);
	}

	public void setGridText(int index, String text) {
		gridItem.setText(index, text);
	}

	public String[] getGridTexts() {
		int length = gridItem.getParent().getColumnCount();
		String[] texts = new String[length];
		for (int i = 0; i < length; i++) {
			texts[i] = gridItem.getText(i);
		}
		return texts;
	}

	public boolean isVisible() {
		return gridItem.isVisible() && ganttItem.isVisible();
	}

	public void setVisible(boolean visible) {
		gridItem.setVisible(visible);
		ganttItem.setVisible(visible);
	}

	@Override
	public Object getData() {
		return ganttItem.getData();
	}

	@Override
	public void setData(Object data) {
		ganttItem.setData(data);
	}

	// ----------------------------------------------- 

	public GanttItem getGanttItem() {
		return ganttItem;
	}

	public GridItem getGridItem() {
		return gridItem;
	}

	public GanttGridTask[] getTasks() {
		GanttTask[] ganttTasks = ganttItem.getTasks();
		GanttGridTask[] tasks = new GanttGridTask[ganttTasks.length];
		for (int i = 0; i < ganttTasks.length; i++) {
			tasks[i] = (GanttGridTask) ganttTasks[i].getData(GanttGridTask.DATA_KEY);
		}
		return tasks;
	}

	public GanttGridItemData getItemData() {
		return new GanttGridItemData(this);
	}

}
